//
//  UIView+HDGoodsCollectController.m
//  app
//
//  Created by D on 15/7/8.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HDGoodsCollectController.h"
#import "HDGoodsCollectManager.h"
#import "HDGoodsCollectResult.h"
#import "HDGoodsCollectCell.h"
#import "HZGoodsDetailViewController.h"

static const CGFloat kCartTotalPanelHeight = 120;

@interface HDGoodsCollectController ()

@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) HDGoodsCollectManager * goodsManager;
@property (nonatomic,strong) HDGoodsCollectResult * goodsResult;
@property (nonatomic,strong) NSString * lastid;

@end

@implementation HDGoodsCollectController

- (void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"我的收藏";
    
    self.navigationController.navigationBar.hidden = NO;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView = [[UITableView alloc]initWithFrame:CGRectOf(CGPointMake(0, 0),
                                                                [self.view.ubo fw:UB_WIDTH fh:UB_HEIGHT]) style:UITableViewStyleGrouped];
    
    
    self.tableView.headerRefreshController = [[UIRefreshHeaderController alloc] initWithTableView:self.tableView caller:[UBTargetCaller of:self action:@selector(refreshDataToGoodsCollect)]];
    
//    self.tableView.footerRefreshController = [[UIRefreshFooterController alloc] initWithTableView:self.tableView caller:[UBTargetCaller of:self action:@selector(loadMoreDataToGoodsCollect)]];
    
    self.tableView.nothingRefreshView = [[UIRefreshNotingRefreshView alloc]initWithView:self.tableView viewController:self tryAgain:@selector(refreshDataToGoodsCollect)];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(self.navigationAndStatusBarHeight, 0, 0, 0);
    self.tableView.contentInset = UIEdgeInsetsMake(self.navigationAndStatusBarHeight, 0, 0, 0);
    [self.view addSubview:self.tableView];

    self.goodsResult = [[HDGoodsCollectResult alloc]initWithTarget:self action:@selector(goodsListLoadData:)];
    self.goodsManager = [HDGoodsCollectManager shareManager];

    
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
   
    self.lastid = @"0";
    
        if (![self.goodsManager fmdbIncludeFromGoodsList] || ![self.goodsManager fmdbIncludeDataFromGoodsList]) {
            [self.tableView.nothingRefreshView show];
            [self resutlGoodsList];
        }else{
            
            if ([self.goodsManager shouldRefreshGoodsListData]) {
                [self resutlGoodsList];
            }else
            {
                self.goodsResult.dataSourceArray = [self.goodsManager arrayFromFmdbGoodsList];
                [self.tableView reloadData];
            }
        }
 
}

#pragma mark - 发送收藏列表请求
-(void)resutlGoodsList
{
    NSDictionary * params = @{@"lastid":self.lastid};
    [self.goodsManager loadOwnerGoodsListWithParams:params result:self.goodsResult Refresh:@"0"];
}
#pragma mark - 刷新
-(void)refreshDataToGoodsCollect
{
    NSDictionary * params = @{@"lastid":@"0"};
    [self.goodsManager loadOwnerGoodsListWithParams:params result:self.goodsResult Refresh:@"1"];
}
#pragma mark - 数据处理
-(void)goodsListLoadData:(HDGoodsCollectResult*)rt
{
    [self.tableView endRefreshingAll];
    if (rt.success) {
        [self.tableView.nothingRefreshView remove];
        self.goodsResult.dataSourceArray = [self.goodsManager arrayFromFmdbGoodsList];
        [self.tableView reloadData];
        self.lastid = [NSString stringWithFormat:@"%d",self.goodsResult.dataSourceArray.count];
        if (self.goodsResult.dataSourceArray.count != 0) {
            NSIndexPath *lastRow = [NSIndexPath indexPathForRow:self.goodsResult.dataSourceArray.count - 1 inSection:0];
            [self.tableView scrollToRowAtIndexPath:lastRow atScrollPosition:UITableViewScrollPositionBottom animated:NO];

        }
        
    }else if (rt.failure)
    {
        [self.tableView.nothingRefreshView requestAgain];
    }
}

#pragma mark - 加载更多
-(void)loadMoreDataToGoodsCollect
{
    
}


#pragma mark - tableView代理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.goodsResult.dataSourceArray.count;
    //    return 13;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentifier = @"cellIdentifier";
    HDGoodsCollectCell * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:cellIdentifier];
     if (cell == nil) {
         cell = [[HDGoodsCollectCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
     }
    HDGoodCollectModel * model = self.goodsResult.dataSourceArray[indexPath.row];
    if (model.delSign.integerValue == 000) {
        [cell cellWithModel:model];
    }
    cell.backgroundView = nil;
    
    return cell;

    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    HDGoodCollectModel * model = self.goodsResult.dataSourceArray[indexPath.row];
    HZGoodsDetailViewController * goodsDetailView = [[HZGoodsDetailViewController alloc]init];
    goodsDetailView.productID = model.product_id;
    [self.navigationController pushViewController:goodsDetailView animated:YES];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UF(208);
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return UF(0.3);
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return UF(0.5);
}


//先要设Cell可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

//进入编辑模式，按下出现的编辑按钮后
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle==UITableViewCellEditingStyleDelete) {
        //        获取选中删除行索引值
        NSInteger row = [indexPath row];
        NSString * pmid = [NSString string];
        HDGoodCollectModel * model = [self.goodsResult.dataSourceArray objectAtIndex:row];
        //        删除单元格的某一行时，在用动画效果实现删除过程
        
        [self.goodsManager updataAndDeleteToDB:model.product_id];
        //        通过获取的索引值删除数组中的值
        [self.goodsResult.dataSourceArray removeObjectAtIndex:row];
        //删除数据库中得数据
//        [self.goodsManager deleteChatListCell:self.goodsResult ChatPmid:model.pmid];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        
        
    }
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
